package li.nibble.twittertido.adapter;

import java.util.ArrayList;

import li.nibble.twittertido.R;
import li.nibble.twittertido.beans.Tweet;
import li.nibble.twittertido.lazyimage.ImageLoader;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TweetRemoteAdapter extends ArrayAdapter<Tweet> {

	private ArrayList<Tweet> items;

	LayoutInflater vi;
	public ImageLoader imageLoader;

	public TweetRemoteAdapter(Context context, int textViewResourceId,
			ArrayList<Tweet> items) {
		super(context, textViewResourceId, items);
		this.items = items;
		vi = (LayoutInflater) getContext().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
	}

	public static class ViewHolder {
		public TextView tweet;
		public TextView user;
		public ImageView image;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		final ViewHolder holder;

		if (v == null) {
			v = vi.inflate(R.layout.tweet_row_remote, null);
			holder = new ViewHolder();
			holder.image = (ImageView) v.findViewById(R.id.avatar_user);
			holder.tweet = (TextView) v.findViewById(R.id.tweet);
			holder.user = (TextView) v.findViewById(R.id.user);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}

		final Tweet tweet = items.get(position);

		holder.tweet.setText(tweet.getTweet());
		holder.user.setText("@" + tweet.getUser());
		imageLoader.DisplayImage(
				"https://api.twitter.com/1/users/profile_image?screen_name="
						+ tweet.getUser() + "&size=normal", holder.image);

		return v;
	}

}
